<template>
	<view>
		<!-- <text v-if="loading">加载中</text> -->
	</view>
</template>

<script>
	
	import Vue from 'vue'
	// import api from '@/common/vmeitime-http/teacher_http'
	// Vue.prototype.$api = api

	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex'
	// import {
	// 	wxAuthorize
	// } from "../../utils/util.js"
	export default {
		data() {
			return {
				loading: false,
			}
		},
		onLoad(options) {
			document.title = "智慧校园"
			// uni.showLoading({  
			//     // title: '加载中'  
			// });
			// const code = this.getUrlParam('state')
			console.log("state.teacher")
			console.log(Vue.prototype.$store.state.teacher)
			if (Vue.prototype.$store.state.teacher.token != null) {
				uni.reLaunch({
					url: "index/index",
					fail: (e) => {
						console.log(e)
					}
				})
			} else {
				this.getCodeLink()
			}
			this.setIsTeacher(true)
		},
		computed: {
			...mapState('teacher', {
      state: state => state
    }),
		},
		methods: {
		    ...mapActions('teacher', [
		      'setToken','setLoginInfo'
		    ]),
			...mapMutations(['setSchoolId', 'setIsTeacher']),
			// 获取code
			getCodeLink() {
				const code = this.getUrlParam('code')
				const state = this.getUrlParam('state')
				console.log('code：' + code)
				console.log('state：' + state)
				if (code == null || code == '') {
					if (process.env.NODE_ENV === 'development') {
						// wxAuthorize();
					}
				} else {
					this.teacherWeChartLogin(code, state)
				}
			},
			teacherWeChartLogin(code, state) {
				this.loading = true;
				this.$api.teacherWeChartLogin({
					code,
					state
				}).then((res) => {
					this.loading = false;
					if (typeof res == "string") { //成功
						this["setToken"](res)
						console.log("token:" + res)
						uni.reLaunch({
							url: "/index/index",
							fail: (e) => {
								console.log(e)
							}
						})
					} else if (res.code == 111005) { //未绑定
						//跳转登录
						var info = JSON.parse(res.msg)
						this["setLoginInfo"](info)
						this.setSchoolId(state)
						console.log("setLoginInfo:" + res)
						console.log(res)
						uni.reLaunch({
							url: 'login/login',
							// url: 'login/login?openId=' + info.openid + '&headimgurl=' + info.headimgurl,
							fail: (e) => {
								console.log(e)
							}
						})
						// JSON.js
						// 	alert("登录错误openid：" + opid)
					} else {
						alert("登录错误code：" + res.msg)
					}
					console.log('request success', res)
					this.res = '请求结果 : ' + JSON.stringify(res);


					// alert(this.res)
					// uni.showToast({
					// 	title: this.res,
					// 	icon: 'success',
					// 	duration: 20000
					// })


				}).catch((err) => {
					this.loading = false;
					console.log('request fail', err);
					uni.showToast({
						title: err.errMsg
					})
				})
			},
			// 解析域名获取code
			getUrlParam(name) {
				let v = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
				var code
				if (v) return decodeURI(v[1])
				// const reg = new RegExp('(^|$)' + name + '=([^&]*)(&|$)')
				// const r = window.location.search.substr(1).match(reg)
				// console.log('getUrlParam')
				// console.log(r[2])
				// // alert(window.location.search.substr(1))
				// if (r !== null) return decodeURI(r[2])
				// return null
			},
		},
		onShow: function() {
			console.log('App Show')
			// console.log(this.token)

		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
</style>
